package newx.com.control.scroll.text
{
	import engine.ui.FaceIconViewUI;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	
	import newx.BaseObject;
	import newx.NxSprite;
	import newx.com.ResManager;
	import newx.com.control.NxClip;
	import newx.com.loader.TxtLoader;
	
	public class NxNewText extends NxSprite
	{
		protected	var textField:TextField;
		
		protected	var scrollBar:NxNewScrollBar;
		protected	var scrollV:int;
		
		protected	var islock:Boolean;
		
		protected	var inited:Boolean;
		
		protected	var WIDTH:int;
		protected	var HEIGHT:int;
		private	var iconText:TextField;
		private	var nowTextHeight:Number;
		private	var textRect:Rectangle;
		private	var iconRect:Rectangle;
		private	var bitSprit:Sprite		=	new Sprite();
		private	var bitMap:BitmapData	=	new BitmapData(1,1);
		private	var wheelData:Number	=	12;
		private	var textStr:String;
		private	var faceArray:Array;
		private	var iconSpace:Sprite;
		public function NxNewText()
		{
			super();
			inited = false;
			WIDTH = 200;
			HEIGHT = 100;
			Init();
		}
		
		private function Init():void
		{
			iconSpace			=	new Sprite();
			textField 			=	new TextField();
			iconText			=	new TextField();
			iconSpace.mouseEnabled	=	false;
			textField.width 	=	WIDTH - 13;
			textField.height 	=	HEIGHT;
			textField.wordWrap	=	true;
			textField.multiline =	true;
			textField.autoSize	=	TextFieldAutoSize.LEFT;
			
			iconText.width 	=	WIDTH - 13;
			iconText.height 	=	HEIGHT;
			iconText.wordWrap	=	true;
			iconText.multiline =	true;
			iconText.autoSize	=	TextFieldAutoSize.LEFT;
			
//			textField.border = true;
//			textField.borderColor = 0x00ff00;
			textField.selectable = false;
			addChild(textField);
			textRect	=	new Rectangle(0,0,WIDTH-13,HEIGHT);
			iconRect	=	new Rectangle(0,0,WIDTH-13,HEIGHT);
			scrollBar = new NxNewScrollBar();
			scrollBar.x = WIDTH - 13;
			scrollBar.bind(scrollChange);
			addChild(scrollBar);
			addChild(iconSpace);
			textField.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
			
			islock = false;
			
			inited = true;	
			iconText.y	=	-500;
			addChild(iconText);
			iconText.visible	=	false;
			reSetSize();
		}
		
		public function set htmlText(s:String):void
		{
			textStr	=	s

			textField.htmlText = 	checkRemoveFaceIconStr(s)
			iconText.htmlText	=	textField.htmlText;
			
			nowTextHeight	=	textHeight;
			scrollBar.setHeight(textRect.height, nowTextHeight);
			showFaceIcon();
			if(s == "")
			{
				clearFaceIcon();
				textRect.y	=	0;
				iconRect.y	=	0;
				scrollmove();
			}
			else
			{
				setScrollV(nowTextHeight> textRect.height>0?(nowTextHeight - textRect.height):0);
			}
		}
		
		public function get htmlText():String
		{
			return textStr;
		}

		public function setScrollV(val:int):void
		{
			if(!islock && val != textRect.y)
			{
				textRect.y	=	val;
				iconRect.y	=	val;
				scrollmove();
			}
		}
		public function jumpLastScrollV(val:int):void
		{
			if( val != textRect.y)
			{
				textRect.y	=	val;
				iconRect.y	=	val;
				scrollmove();
			}
		}

		public function setSizeWH(w:int, h:int):void
		{
			WIDTH = w;
			HEIGHT = h;
			
			if(inited)
				reSetSize();
		}

		public function set LOCK(b:Boolean):void
		{
			islock = b;
		}
		
		public function get LOCK():Boolean
		{
			return islock;
		}
		
		public function addLinkClickHandel(val:Function):void
		{
			data.HandleProperties("onLinkHandle",val);
			textField.addEventListener(TextEvent.LINK,onLinkHandle);
		}
		
		public function removeLinkClickHandel():void
		{
			data.DeleteProperties("onLinkHandle");
			textField.removeEventListener(TextEvent.LINK,onLinkHandle);
		}
		public function get maxScrollV():int
		{
			
			return textField.textHeight + 4- textRect.height;
		}
		
		private function showFaceIcon():void
		{
			clearFaceIcon();
			var indexArray:Array	=	[];
			var str:String	=	textField.text;
			for(var index:int = 0; index < str.length; index++)
			{
				if(str.charAt(index) == "　")
				{
					indexArray.push(index);
				}
			}
			for(var i:int = 0; i <indexArray.length; i++)
			{
				var iconBox:Rectangle	=	iconText.getCharBoundaries(indexArray[i]);
				var iconStr:String	=	"res/swf/faceIcon/"+"bq" + (faceArray[i] as String).substr(1,(faceArray[i] as String).length)+".swf";
				var icon:MovieClip	=	FaceIconViewUI.getInstance().borrowFaceIcon(iconStr)
				if(iconBox != null && icon != null)
				{
					icon["iconName"]			=	iconStr;	
					icon.x	=	iconBox.x + (iconBox.width - 32) * .5;
					icon.y	=	iconBox.y + (iconBox.height - 32);
					if(icon.y < iconText.textHeight - 100)
					{
						icon.gotoAndStop(1);
					}
					icon.mouseEnabled	=	false;
					iconSpace.addChild(icon);
				}
			}
			
		}
		private function box():Sprite
		{
			var tempShape:Sprite	=	new Sprite();
			tempShape.graphics.beginFill(0xFF8800);
			tempShape.graphics.drawRect(0,0,16,16);
			tempShape.graphics.endFill();
			return tempShape;
		}
		private function clearFaceIcon():void
		{
			while(iconSpace.numChildren > 0)
			{
				var tempIcon:DisplayObject	=	iconSpace.getChildAt(0) 
				iconSpace.removeChild(tempIcon);
				FaceIconViewUI.getInstance().returnFaceIcon(tempIcon["iconName"],tempIcon as MovieClip)
			}
		}
		private function getStr(val:String):String
		{
			var tempStr:String	=	val;
			
			return tempStr;
		}
		private function checkRemoveFaceIconStr(val:String):String
		{
			val	=	val.replace(/\　/g, " ");
			faceArray	=	[];
			faceArray	=	val.match(/\*([0-1][0-9])/g);
			val = val.replace(/\*([0-1][0-9])/g, "<font size='32'>　</font>");
			return val;
		}

		private function onLinkHandle(e:TextEvent):void
		{
			var tFunc:Function;
			
			if(data.CheckProperties("onLinkHandle"))
			{
				tFunc	=	data.GetProperties("onLinkHandle") as Function; 
				tFunc.call(this,e.text);
				tFunc	=	null;
				//data.DeleteProperties("onLinkHandle");
			}
		}
		
		private function get textHeight():Number
		{
			var tempHeight:Number;
			textField.scrollRect	=	null;
			bitMap.draw(bitSprit);
			tempHeight				=	textField.height;
			textField.scrollRect	=	textRect;
			return tempHeight;
		}

		private function scrollChange():void
		{
			textRect.y 				=	scrollBar.Ratio ;
			iconRect.y				=	scrollBar.Ratio
			textField.scrollRect	=	textRect;
			iconSpace.scrollRect	=	iconRect
		}
		
		private function reSetSize():void
		{
			textField.width 		=	WIDTH - 13;
			iconText.width			=	WIDTH - 13;
			textRect.width			=	WIDTH -13;
			textRect.height			=	HEIGHT
			iconRect.width			=	WIDTH -13;
			iconRect.height			=	HEIGHT
			textField.scrollRect	=	textRect;
			iconSpace.scrollRect	=	iconRect
			scrollBar.x 			=	WIDTH - 13;
			scrollBar.setHeight(textRect.height, nowTextHeight);
		}
		
		private function mouseWheel(e:MouseEvent):void
		{
			var tempY:int	=	textRect.y;
			tempY	-= e.delta * wheelData;
			if(tempY < 0)
			{
				tempY	=	0;
			}
			else if(tempY >= (nowTextHeight - HEIGHT))
			{
				tempY	=	nowTextHeight - HEIGHT;
			}
			if(tempY != textRect.y)
			{
				textRect.y	=	tempY;
				scrollmove();
			}
		}
		
		private function scrollmove(e:Event=null):void
		{
			var ratio:int 			= textRect.y;
			scrollBar.Ratio = ratio;
		}
		
	}
}